﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using HZ.MBSM.DBUtility;

public partial class Supervise_TraGoods : System.Web.UI.Page, HZ.MBSM.BLL.TraDes
{
    public string _strUserType = "", _AreaId = "", _strAccountId = "", _strEndDate = "", _strStartDate = "";
    bool faly = false;//是否是生产企业；
    protected void Page_Load(object sender, EventArgs e)
    {
        string strSql, _SortId = "";
                
        if (HZ.MBSM.BLL.Login.CheckAssert(this.UseAccountType))
        {

            HZ.MBSM.Model.Account _Account = (HZ.MBSM.Model.Account)Session["Account"];
            if (_Account != null)
            {
                _AreaId = SysFun.ToTrim(_Account.AreaId);
                _strUserType = SysFun.ToTrim(_Account.Type);
                _strAccountId = SysFun.ToTrim(_Account.AccountId);
                //如果当前使用人是配送企业
                if (_strUserType == "2")
                {
                    _AreaId = "530000";
                }
                if (_Account.Sort == "0")
                {
                    faly = true;
                    txtCompanyNameSC.Text = _Account.DWName;
                    txtCompanyNameSC.ReadOnly = true;
                    txtUserCodeSC.Text = _Account.UserCode;
                    txtUserCodeSC.ReadOnly = true;
                }
                else
                {
                    txtUserCodeSC.ReadOnly = false;
                    txtCompanyNameSC.ReadOnly = false;
                    faly = false;
                }

            }
            if (_strUserType == "3")
            {
                //如果当前用户是医院，那么就把地区隐藏
                labArea.Visible = false;
                Label1.Visible = false;
            }
            else
            {
                //如果是监督机构或中介就根据其所在的地区来选择
                //如果是省一级的监督机构就可以看到整个云南省的信息,如果是某个市的监督机构就只能看到本市的信息
                System.Text.StringBuilder strHtml = new System.Text.StringBuilder();
                string _strAreaNameForm, _strAreaIdForm;
                _strAreaNameForm = SysFun.IsSQL(SysFun.ToTrim(Request.Form["txtAreaName"]));
                _strAreaIdForm = SysFun.IsSQL(SysFun.ToTrim(Request.Form["txtAreaId"]));
                if (_strAreaNameForm != "" && _strAreaIdForm != "")
                {
                    strHtml.Append("<input type='text' name='txtAreaName' value ='" + _strAreaNameForm + "' id='Text1' language='javascript' onclick='return Text1_onclick()'  InvertedComma='所属地区' readonly />");
                    strHtml.Append("<input type='text' name='txtAreaId'  value ='" + _strAreaIdForm + "' style='display: none' />");
                }
                else
                {
                    strHtml.Append("<input type='text' name='txtAreaName' id='Text1' language='javascript' onclick='return Text1_onclick()'  InvertedComma='所属地区' readonly />");
                    strHtml.Append("<input type='text' name='txtAreaId' style='display: none' />");
                }
                if (_AreaId != "")
                {
                    strHtml.Append("<a href='javascript:AreaSelect(" + _AreaId + ")'>选择地区</a>");
                }
                labArea.Text = strHtml.ToString();
                labArea.Visible = true;
                Label1.Visible = true;
            }
            if (!IsPostBack)
            {
                lstQualityLevals.Items.Add("全部");
                lstQualityLevals.Items.Add("普通GMP药品");
                lstQualityLevals.Items.Add("专利药品");
                lstQualityLevals.Items.Add("1999年以来获得国家科学技术奖的药品");
                lstQualityLevals.Items.Add("国家一类新药");
                lstQualityLevals.Items.Add("原研制药品");
                lstQualityLevals.Items.Add("单独定价药品");
                lstQualityLevals.Items.Add("优质优价中成药");
                lstQualityLevals.Items.Add("全国医药工业百强企业的药品");
                lstQualityLevals.Items.Add("中药保护品种");
                lstQualityLevals.Items.Add("欧美认证药品");
                //<!--初始化统计时间
                int Year, Month, Day, DMonth, Year1;
                Year = DateTime.Now.Year;
                Year1 = Year;
                DMonth = DateTime.Now.Month;
                Month = DMonth - 1;
                if (Month == 0)
                {
                    Year1 -= 1;
                    Month = 12;
                }

                Day = DateTime.Now.Day;
                txtStartDate.Text = Year1.ToString() + "-" + Month.ToString() + "-01";
                txtEndDate.Text = Year.ToString() + "-" + DMonth.ToString() + "-01";

                _strEndDate = SysFun.ToTrim(Request.QueryString["EndDate"]);
                _strStartDate = SysFun.ToTrim(Request.QueryString["StartDate"]);
                if (_strEndDate != "")
                {
                    txtEndDate.Text = _strEndDate;
                }
                if (_strStartDate != "")
                {
                    txtStartDate.Text = _strStartDate;
                }
                //初始化统计时间-->


                Db DbProject = new Db();
                System.Data.IDataReader dr;

                //<!--初始化大类
                int i = 0;
                strSql = "select sortid,sortname from product_sort ";
                dr = DbProject.DataReader(strSql);
                while (dr.Read())
                {
                    lstSort.Items.Add(dr["sortname"].ToString());
                    lstSort.Items[i++].Value = dr["sortid"].ToString();
                }
                lstSort.DataBind();
                dr.Close();
                dr.Dispose();
                _SortId = SysFun.ToTrim(Request.QueryString["SortId"]);
                if (_SortId != "")
                {
                    lstSort.SelectedValue = _SortId;
                }
                //初始化大类-->
                DbProject.Close();


            }

            if (Request.Form["butSelectAll"] == "查看全部")
            {
                //显示所有符合条件的药品信息
                strSql = @"select  SupplyGoodsId,GoodsId,productname,GoodsName,medicinemodel,outlookc,factor,PackUnit,companyName_SC,
                            CASE QualityLevels " + ConfigurationManager.AppSettings["QualityLevels"].ToString() + " END AS  QualityLevels,convert(numeric(20,2),BidPrice) as BidPrice,sum(inflownum) as DGSL ,  convert(numeric(20,2),sum(inflownum*TradePrice)) as DHJE  from tra_GoodsId where  {0}  group by SupplyGoodsId,GoodsId,productname,GoodsName,medicinemodel,outlookc,                            factor,PackUnit,companyName_SC,QualityLevels,BidPrice order by DHJE DESC,SupplyGoodsId";
                string _strtempWhere = fctConditions();
                strSql = String.Format(strSql, _strtempWhere);
                Db db1 = new Db();
                System.Data.IDataReader dr;
                dr = db1.DataReader(strSql);
                GridView1.DataSource = dr;
                GridView1.DataBind();
                dr.Close();
                dr.Dispose();
                db1.Close();
            }
            else
            {
                showGridView();
            }
        }
    }

    protected void Button5_Click(object sender, EventArgs e)
    {

    }

    /// <summary>
    /// 查询总共有多少页
    /// </summary>
    /// <returns></returns>
    /// <summary>
    /// 将数据绑定到GridView1
    /// </summary>
    /// <param name="PageIndex">转向第几页</param>
    public void showGridView()
    {

        string strConditions,
            _SelectField = @"SupplyGoodsId,GoodsId,productname,GoodsName,medicinemodel,outlookc,factor,PackUnit,companyName_SC,
                            CASE QualityLevels " + ConfigurationManager.AppSettings["QualityLevels"].ToString() + " END AS  QualityLevels,convert(numeric(20,2),BidPrice) as BidPrice,sum(inflownum) as DGSL ,      convert(numeric(20,2),sum(inflownum*TradePrice)) as DHJE,case DealStatus  when 0 then '未确认送货' when 1 then '送货中' when 2 then '已入库' when 4 then '无法配送' when 5 then '到到期未送' end as DealStatus,convert(varchar(100),HospitalInflowDate,23) as HospitalInflowDate,ispayment";
        strConditions = fctConditions();
        Db db = new Db();
        DbPage dbPage = new DbPage(db);
        dbPage.Table = "tra_GoodsId";                                               //要查询的表明
        dbPage.SelectFieldGroup = "SupplyGoodsId,GoodsId,productname,GoodsName,medicinemodel,outlookc,factor,PackUnit,companyName_SC,QualityLevels,BidPrice,DealStatus,HospitalInflowDate,ispayment";
        dbPage.Key = "SupplyGoodsId";                                               //表的主键    
        dbPage.Where = strConditions;                                           //查询条件    
        dbPage.SelectField = _SelectField;//要显示的字段
        dbPage.PageIndex = SysFun.ToInt(Request["CurrentlyPageIndex"]);          //要显示第几页   
        dbPage.PageSize = 30;                                                    //没有显示记录条数   
        dbPage.OrderBy = "DHJE,SupplyGoodsId";                  //排序字段
        dbPage.OrderByIsDesc = true;
        dbPage.DGJE = "convert(numeric(20,2),sum(GoodsNum*TradePrice)) as DGJE";
        dbPage.DHJE = "convert(numeric(20,2),sum(inflownum*TradePrice)) as DHJE";
        System.Data.IDataReader dr = dbPage.ExecuteReaderGroup(60);
        GridView1.DataSource = dr;
        GridView1.DataBind();
        dr.Close();
        dr.Dispose();
        dbPage.Db.Close();
        db.Close();
        this.DbPageFlip1.DbPage = dbPage;//将数据传给控键
    }
    /// <summary>
    /// 生成查询条件
    /// </summary>
    /// <returns></returns>
    public string fctConditions()
    {

        string _strTemp = " DealStatus>=0 and DealStatus<>3 and", _strProductName, _strMedicinemodel, _strOutlookc, _strGoodsName, _QualityLevals, _GoodsId,_ispayment,
                _strSortId = "", _strCompanyNameSC, _UserCodeSC, _strAreaIdForm;
        //通用名
        _strProductName = txtProductName.Text.ToString();
        if (_strProductName != "")
        {
            _strTemp += "   ( ProductName like '%" + _strProductName + "%' or Product_SpellingCode like '%" + _strProductName + "%' )  and";
        }

        //剂型
        _strMedicinemodel = txtMedicinemodel.Text.ToString();
        if (_strMedicinemodel != "")
        {
            _strTemp += " medicinemodel like '%" + _strMedicinemodel + "%' and";
        }

        //规格
        _strOutlookc = txtOutlook.Text.ToString();
        if (_strOutlookc != "")
        {
            _strTemp += " outlookc like '%" + _strOutlookc + "%' and";
        }

        //商品名
        _strGoodsName = txtGoodsName.Text.ToString();
        if (_strGoodsName != "")
        {
            _strTemp += " goodsname like '%" + _strGoodsName + "%' and";
        }

        //质量层次
        _QualityLevals = lstQualityLevals.SelectedValue;
        switch (_QualityLevals)
        {
            case "全部":
                break;
            case "普通GMP药品":
                _strTemp += " qualitylevels=0 and";
                break;
            case "专利药品":
                _strTemp += " qualitylevels=1 and";
                break;
            case "1999年以来获得国家科学技术奖的药品":
                _strTemp += " qualitylevels=2 and";
                break;
            case "国家一类新药":
                _strTemp += " qualitylevels=3 and";
                break;
            case "原研制药品":
                _strTemp += " qualitylevels=4 and";
                break;
            case "单独定价药品":
                _strTemp += " qualitylevels=5 and";
                break;
            case "优质优价中成药":
                _strTemp += " qualitylevels=6 and";
                break;
            case "全国医药工业百强企业的药品":
                _strTemp += " qualitylevels=7 and";
                break;
            case "中药保护品种":
                _strTemp += " qualitylevels=8 and";
                break;
            case "欧美认证药品":
                _strTemp += " qualitylevels=9 and";
                break;
        }

        //药品流水号
        _GoodsId = txtGoodsId.Text.ToString();
        if (_GoodsId != "")
        {
            _strTemp += " GoodsId =" + _GoodsId + " and";
        }

        //生产企业名称
        _strCompanyNameSC = txtCompanyNameSC.Text.ToString();
        if (_strCompanyNameSC != "")
        {
            _strTemp += " CompanyName_SC like '%" + _strCompanyNameSC + "%' and";
        }

        //生产企业编码
        _UserCodeSC = txtUserCodeSC.Text.ToString();
        if (_UserCodeSC != "")
        {
            _strTemp += " UserCode_SC like '%" + _UserCodeSC + "%' and";
        }


        //要统计的时间范围
        _strEndDate = txtEndDate.Text.ToString();
        _strStartDate = txtStartDate.Text.ToString();
        if (_strEndDate != "" && _strStartDate != "")
        {
            _strTemp += " isnull(hospitalinflowdate,'3000-12-30') >='" + _strStartDate + "' and isnull(hospitalinflowdate,'1900-01-01') <='" + _strEndDate + "' and";
        }

        //如果当前使用人是配送企业
        if (_strUserType == "2")
        {
            if (faly)
            { 
                //配送人是生产企业 能查自己生产和配送的药品
                _strTemp += " (AccountId_PS=" + _strAccountId + " or accountid_sc=" + _strAccountId + ") and";
            }
            else
            {
                //配送人是经营企业 能看自己配送的药品
                _strTemp += " AccountId_PS=" + _strAccountId + " and";
            }
        }

        //根据登录人和地区确定医院范围
        if (_strUserType == "3")
        {
            _strTemp += "  AccountId_YY=" + _strAccountId + " and";
        }
        else
        {

            _strAreaIdForm = SysFun.ToTrim(Request.Form["txtAreaId"]);
            if (_strAreaIdForm != "" && _strAreaIdForm != "530000")
            {
                _AreaId = _strAreaIdForm;
            }
            _strTemp += SysFun.AreaIdWhere(_AreaId);
        }

        //结算
        _ispayment = DropDownList1.SelectedItem.Text;
        if (_ispayment == "未结算")
        {
            _strTemp += " (ispayment=''  or ispayment is null) and";
        }
        if (_ispayment == "已结算")
        {
            _strTemp += " ispayment=1 and";
        }
        //药品类别
        _strSortId = lstSort.SelectedValue;
        if (_strSortId != "0")
        {
            _strTemp += " ( SortId=" + _strSortId + " or FrontSortId=" + _strSortId + " ) and";
        }

        //价格范围
        string _strMinTradePrice, _strMaxTradePrice;
        _strMinTradePrice = txtMinTradePrice.Text.ToString();
        if (_strMinTradePrice != "")
        {
            _strTemp += "  TradePrice>=" + _strMinTradePrice + " and";
        }
        _strMaxTradePrice = txtMaxTradePrice.Text.ToString();
        if (_strMaxTradePrice != "")
        {
            _strTemp += "  TradePrice<=" + _strMaxTradePrice + " and";
        }

        if (dealDrop.SelectedValue != "")
        {
            
            if (dealDrop.SelectedValue == "0")
            {
                _strTemp += " DealStatus=" + dealDrop.SelectedValue + " and buystatus=2 and";
            }
            else
            {
                _strTemp += " DealStatus=" + dealDrop.SelectedValue + " and";
            }

        }

        if (_strTemp != "")
        {
            _strTemp = "  " + _strTemp.Substring(0, _strTemp.Length - 3);
        }

        return _strTemp;

    } 
    protected void ToEcxul_Click(object sender, EventArgs e)
    {

        Db db = new Db();
        System.Data.IDataReader dr;
        string _strSql = "Select  GoodsId as 流水号,productname as 通用名,GoodsName as 商品名,medicinemodel as 剂型,outlookc as 规格,factor as 转换系数,PackUnit as 质量层次,companyName_SC as 生产企业,CASE QualityLevels " + ConfigurationManager.AppSettings["QualityLevels"].ToString() + " END AS  质量层次,convert(numeric(20,2),BidPrice) as 中标价,sum(inflownum) as 到货数量 ,  convert(numeric(20,2),sum(inflownum*TradePrice)) as 到货金额,case DealStatus  when 0 then '未确认送货' when 1 then '送货中' when 2 then '已入库' when 4 then '无法配送' when 5 then '到到期未送' end as 采购状态,HospitalInflowDate as 入库时间,case isnull(ispayment,0) when 0 then '未结算' when 1 then '已结算' end as '结算状态'  From tra_GoodsId Where     " + fctConditions() + "  Group By SupplyGoodsId,GoodsId,productname,GoodsName,medicinemodel,outlookc,factor,PackUnit,companyName_SC,QualityLevels,BidPrice,DealStatus Order By 到货金额 Desc,HospitalInflowDate,ispayment";

        dr = db.DataReader(_strSql);
        this.GridView2.DataSource = dr;
        this.GridView2.DataBind();
        db.Close();
        dr.Close();
        dr.Dispose();
        SysFun.WriteToExcel(GridView2, this.Page, "按药品查询.xls");



    }
    public override void VerifyRenderingInServerForm(Control control)
    {
        //OverRide　为了使导出成Excel可行！
    }
    #region TraDes 成员

    public string Name
    {
        get { return "按药品查询"; }
    }

    public string Describe
    {
        get { return ""; }
    }

    public string ParentName
    {
        get { return "交易查询"; }
    }

    public System.Collections.Generic.List<HZ.MBSM.DAL.AccountType> UseAccountType
    {
        get
        {
            System.Collections.Generic.List<HZ.MBSM.DAL.AccountType> _AccountType = new System.Collections.Generic.List<HZ.MBSM.DAL.AccountType>();
            _AccountType.Add(HZ.MBSM.DAL.AccountType.enumHospitale);
            _AccountType.Add(HZ.MBSM.DAL.AccountType.enumAttorney);
            _AccountType.Add(HZ.MBSM.DAL.AccountType.enumSupervise);
            _AccountType.Add(HZ.MBSM.DAL.AccountType.enumCompany);
            _AccountType.Add(HZ.MBSM.DAL.AccountType.enumSuperviseChildCheck);
            return _AccountType;
        }
    }
    #endregion
   
}
